package producer;


import data.handle.PackMessage;
import local.LocalInstance;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Properties;

public class PartAndLineItemHandler implements Handler {
    private static int portNum;//另外三个远程实例每个实例开的端口数量，每个端口对应零件号的一段范围。
    private static int partKeyNum;//每个端口需要处理的零件号的数量
    private static int fileReaderThreadNum;
    private SocketChannel[] clients;//一组TCP连接
    private static int instanceId;

    public static void init(int id){
        instanceId = id;
        try {
            InputStream is = PartAndLineItemHandler.class.getClassLoader().getResourceAsStream("address.properties");
            Properties properties = new Properties();
            properties.load(is);
            portNum = Integer.parseInt(properties.getProperty("portNum"));
            partKeyNum = 2000000 / (portNum * 4);
            fileReaderThreadNum = Integer.parseInt(properties.getProperty("fileReaderThreadNum"));
            LocalInstance.startLocalThreads(portNum, fileReaderThreadNum);//开启本地数据接收和处理的线程
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public PartAndLineItemHandler(int num) {
//        clients = ProduceLink.getOneGroupClients();
        fileReaderThreadNum = num;
    }


//    @Override
//    public void localHandle(ByteBuffer byteBuffer, int index, int fileIndex) {//本地处理
//        LocalInstance.addData(byteBuffer, index, fileIndex);
//    }

    @Override
    public void localHandle(PackMessage packMessage, int index, int fileIndex) {//本地处理
        LocalInstance.addData(packMessage, index, fileIndex);
//        System.out.println("localHandle");
    }

    @Override
    public void remoteHandle(ByteBuffer byteBuffer, int index) {//远程处理
//        SocketChannel client = clients[index - portNum];
//        try {
//            byteBuffer.flip();
//            while (byteBuffer.hasRemaining()) {
//                client.write(byteBuffer);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        System.out.println("remoteHandle");
    }

    @Override
    public void localFile1End(){
//        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2);
//        short b = (short) 0xfff0;
//        byteBuffer.putShort(b);
//        for (int i = 0; i < portNum; i++) {
//            LocalInstance.addData(byteBuffer, i, 0);
//        }
        for (int i = 0; i < portNum; i++) {
            PackMessage packMessage = new PackMessage();
            packMessage.setEndFlag(true);
            LocalInstance.addData(packMessage, i, 0);
        }

        System.out.println("localFile1End");
    }



    @Override
    public void localFile2End(){
//        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2);
//        short b = (short) 0xffff;
//        byteBuffer.putShort(b);
//        for (int i = 0; i < portNum; i++) {
//            LocalInstance.addData(byteBuffer, i, 1);
//        }

        for (int i = 0; i < portNum; i++) {
            PackMessage packMessage = new PackMessage();
            packMessage.setEndFlag(true);
            LocalInstance.addData(packMessage, i, 1);
        }
        System.out.println("localFile2End");
    }



    public static int getIndex(int partKey){
        return (partKey - 1) / partKeyNum;//计算该零件号属于哪个分段（0 ~ portNum-1分段在本机处理，其余分段远程分布式处理）
    }

    public static boolean isLocalHandle(int index){
        return index >= instanceId * portNum && index < instanceId * portNum + portNum;
    }

    public static int getLocalIndex(int index){
        return index - instanceId * portNum;
    }
}
